//
//  MTNewPaymentFooterView.swift
//  MT
//
//  Created by ooops on 2020/5/14.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol MTNewPaymentFooterViewDelegate: NSObjectProtocol {
    func didTapAgreenButton(sender: UIButton)
    func didTapAgreenmentProtocol()
}

class MTNewPaymentFooterView: UICollectionReusableView {
    
    weak var delegate: MTNewPaymentFooterViewDelegate?
    
    class func reuseIdentity() -> String {
        return "MTNewPaymentFooterViewId"
    }
    
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(10)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "未成年用户在觅颜进行充值，必须得到家长或其监护人同意"
        return l
    }()
    
    private lazy var agreeButton: UIButton = {
        let _agreeButton: UIButton = UIButton(frame: CGRect.zero)
        //_agreeButton.setTitle("", for: UIControl.State.normal)
        _agreeButton.setImage(UIImage.init(named: "home_question_option_default"), for: UIControl.State.normal)
        _agreeButton.setImage(UIImage.init(named: "btn_choose"), for: UIControl.State.selected)
        _agreeButton.setTitleColor(UIColor.init(hex: 0x474747), for: UIControl.State.normal)
        _agreeButton.addTarget(self, action: #selector(agree(sender:)), for: UIControl.Event.touchUpInside)
        _agreeButton.titleLabel?.font = PingFangSCRegularFont(10)
        _agreeButton.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
//        _agreeButton.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 14)
//        _agreeButton.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 14)
        return _agreeButton
    }()
    
    lazy var agreeLabel: YYLabel = {
        let l = YYLabel()
        l.font = PingFangSCRegularFont(10)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "《用户充值协议》"
        l.isUserInteractionEnabled = true
        return l
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        setRichLabel()
    }
    
    private func setupUI() {
        addSubview(tipLabel)
        addSubview(agreeButton)
        addSubview(agreeLabel)
    }
    
    @objc func agree(sender: UIButton) {
        sender.isSelected = !sender.isSelected
        //self.confirmButton.isEnabled = sender.isSelected
        //self.confirmButton.isUserInteractionEnabled = sender.isSelected
        if sender.isSelected { //选中s
            //self.confirmButton.backgroundColor = UIColor.init(hex: 0xFF4F58)
        } else {
            //self.confirmButton.backgroundColor = UIColor.init(hex: 0xA5A4A4)
        }
        self.delegate?.didTapAgreenButton(sender: sender)
    }
    
    func setRichLabel() {
        //设置整段字符串的颜色
        let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key(rawValue: "NSFontAttributeName"): PingFangSCRegularFont(10), NSAttributedString.Key(rawValue: "NSForegroundColorAttributeName"): UIColor(hex: 0x426bf2)]
        
        let attributedText: NSMutableAttributedString = NSMutableAttributedString.init(string: "我已阅读并同意 《用户充值协议》", attributes: attributes)
        //设置高亮色和点击事件
        attributedText.setTextHighlight(attributedText.string.nsrange(of: "《用户充值协议》")[0], color: UIColor(hex: 0x426bf2), backgroundColor: nil, tapAction: { (containerView, text, range, rect) in
            //self.delegate?.userAgreement()
            self.delegate?.didTapAgreenmentProtocol()
        }, longPressAction: { (view, text, range, rect) in
            
        })
//
//        attributedText.setUnderlineStyle(.single, range: attributedText.string.nsrange(of: "《用户协议》")[0])
//        attributedText.setUnderlineStyle(.single, range: attributedText.string.nsrange(of: "《隐私政策》")[0])
//
//        attributedText.setUnderlineColor(.red, range: attributedText.string.nsrange(of: "《用户协议》")[0])
//        attributedText.setUnderlineColor(.red, range: attributedText.string.nsrange(of: "《隐私政策》")[0])
        
        self.agreeLabel.attributedText = attributedText
    }
    
    private func makeConstraint() {
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(30)
        }
        
        agreeButton.snp.makeConstraints { (make) in
            make.right.equalTo(agreeLabel.snp.left).offset(20)
            make.centerY.equalTo(agreeLabel)
            make.width.height.equalTo(40)
        }
        
        agreeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(8)
            make.centerX.equalToSuperview().offset(20)
            //make.left.equalTo(agreeButton.snp.right).offset(2)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}
